//1. 安装
//引入 mongoose
const mongoose = require('mongoose');

//2. 连接mongodb数据库  connect 连接
mongoose.connect('mongodb://127.0.0.1/project', { useNewUrlParser: true,useUnifiedTopology: true });

//3. 设置连接的回调
mongoose.connection.on('open', () => {
    //4. 声明文档结构
    const PhoneSchema = new mongoose.Schema({
        brand: String,
        color: String,
        price: Number,
        tags: Array,
    });
    //5. 创建模型对象           集合的名称 创建集合时会变成『复数』
    const PhoneModel = mongoose.model('phone', PhoneSchema);
    
    //个性化读取
    //属性筛选
    // PhoneModel.find().select({brand: 1, price: 1, _id: 0}).exec((err, data) => {
    //     if(err) throw err;
    //     console.log(data);
    // });

    //排序 sort 排序
    // PhoneModel.find().select({brand:1, price: 1}).sort({price: 1}).exec((err, data) => {
    //     if(err) throw err;
    //     console.log(data);
    // });

    //分页 截断  skip 跳过   limit 限定
    PhoneModel.find().skip(2).limit(2).select({brand:1, price:1}).sort({price: -1}).exec((err, data) => {
        if(err) throw err;
        console.log(data);
    });

});